@echo off
setlocal enabledelayedexpansion

REM FTP客户端测试脚本 (Windows版本)
echo 🧪 FTP客户端测试脚本
echo ====================

REM 设置颜色 (Windows 10+)
for /f %%i in ('echo prompt $E ^| cmd') do set "ESC=%%i"
set "GREEN=%ESC%[32m"
set "RED=%ESC%[31m"
set "YELLOW=%ESC%[33m"
set "NC=%ESC%[0m"

REM 函数：打印成功消息
:print_success
echo %GREEN%✅ %~1%NC%
goto :eof

REM 函数：打印错误消息
:print_error
echo %RED%❌ %~1%NC%
goto :eof

REM 函数：打印警告消息
:print_warning
echo %YELLOW%⚠️ %~1%NC%
goto :eof

REM 函数：打印信息消息
:print_info
echo %YELLOW%ℹ️ %~1%NC%
goto :eof

REM 检查Rust是否安装
:check_rust
call :print_info "检查Rust环境..."
where cargo >nul 2>&1
if %errorlevel% equ 0 (
    call :print_success "Rust环境正常"
    cargo --version
) else (
    call :print_error "Rust未安装，请先安装Rust"
    exit /b 1
)
goto :eof

REM 构建客户端
:build_client
call :print_info "构建FTP客户端..."
cargo build -p ftp-client
if %errorlevel% equ 0 (
    call :print_success "客户端构建成功"
) else (
    call :print_error "客户端构建失败"
    exit /b 1
)
goto :eof

REM 测试客户端连接
:test_client_connection
call :print_info "测试客户端连接..."

REM 测试连接到不存在的服务器（应该失败）
call :print_info "测试连接到不存在的服务器..."
cargo run -p ftp-client -- --host 127.0.0.1 --port 9999 2>&1 | findstr "Failed to connect" >nul
if %errorlevel% equ 0 (
    call :print_success "连接失败测试通过"
) else (
    call :print_warning "连接失败测试可能有问题"
)

REM 测试连接到FTP服务器（如果运行）
call :print_info "测试连接到FTP服务器..."
cargo run -p ftp-client -- --host 127.0.0.1 --port 21 2>&1 | findstr "Connected to" >nul
if %errorlevel% equ 0 (
    call :print_success "连接成功测试通过"
) else (
    call :print_warning "FTP服务器可能未运行"
)
goto :eof

REM 测试客户端认证
:test_client_auth
call :print_info "测试客户端认证..."

REM 测试无效认证
call :print_info "测试无效认证..."
cargo run -p ftp-client -- --host 127.0.0.1 --port 21 --username invalid --password wrong 2>&1 | findstr "Login failed" >nul
if %errorlevel% equ 0 (
    call :print_success "无效认证测试通过"
) else (
    call :print_warning "无效认证测试可能有问题"
)

REM 测试有效认证（如果服务器运行且有测试用户）
call :print_info "测试有效认证..."
cargo run -p ftp-client -- --host 127.0.0.1 --port 21 --username testuser --password testpass 2>&1 | findstr "Logged in" >nul
if %errorlevel% equ 0 (
    call :print_success "有效认证测试通过"
) else (
    call :print_warning "有效认证测试失败（可能需要先启动服务器和创建用户）"
)
goto :eof

REM 测试交互模式
:test_interactive_mode
call :print_info "测试交互模式..."

REM 创建测试输入文件
echo pwd > test_input.txt
echo ls >> test_input.txt
echo cd / >> test_input.txt
echo pasv >> test_input.txt
echo type A >> test_input.txt
echo quit >> test_input.txt

REM 运行交互模式测试
cargo run -p ftp-client -- --host 127.0.0.1 --port 21 --username testuser --password testpass --interactive < test_input.txt 2>&1 | findstr "FTP Client Commands" >nul
if %errorlevel% equ 0 (
    call :print_success "交互模式测试通过"
) else (
    call :print_warning "交互模式测试失败"
)

REM 清理测试文件
del test_input.txt 2>nul
goto :eof

REM 运行完整测试
:run_full_test
call :print_info "开始FTP客户端完整测试"

call :check_rust
call :build_client
call :test_client_connection
call :test_client_auth
call :test_interactive_mode

call :print_success "所有测试完成！"
goto :eof

REM 显示帮助信息
:show_help
echo 用法: %~nx0 [选项]
echo.
echo 选项:
echo   build      - 构建客户端
echo   connection - 测试连接
echo   auth       - 测试认证
echo   interactive - 测试交互模式
echo   test       - 运行所有测试
echo   help       - 显示此帮助信息
echo.
echo 示例:
echo   %~nx0 build      # 构建客户端
echo   %~nx0 test       # 运行所有测试
goto :eof

REM 主函数
:main
if "%1"=="" set "1=test"
if "%1"=="build" (
    call :check_rust
    call :build_client
) else if "%1"=="connection" (
    call :check_rust
    call :test_client_connection
) else if "%1"=="auth" (
    call :check_rust
    call :test_client_auth
) else if "%1"=="interactive" (
    call :check_rust
    call :test_interactive_mode
) else if "%1"=="test" (
    call :run_full_test
) else if "%1"=="help" (
    call :show_help
) else (
    call :print_error "未知选项: %1"
    call :show_help
    exit /b 1
)
goto :eof

REM 运行主函数
call :main %*
